package cn.huifu.management.controller;

import cn.huifu.management.mapper.UserMapper;
import cn.huifu.management.pojo.dto.UserLoginDTO;
import cn.huifu.management.pojo.dto.UserRegDTO;
import cn.huifu.management.pojo.entiry.User;
import cn.huifu.management.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;
    @RequestMapping("/login")
    public int login(@RequestBody UserLoginDTO userLoginDTO, HttpSession session){
        UserVO userVO =mapper.selectUserName(userLoginDTO.getUsername());
        if(userVO!=null){
            if(userVO.getPassword().equals(userLoginDTO.getPassword())){
                session.setAttribute("user",userVO);
                return 1;
            }
            return 2;
        }
        return 3;
    }
    //业主注册使用
    @RequestMapping("/reg")
    public int reg(@RequestBody UserRegDTO userRegDTO){
        UserVO userVO =mapper.selectUserName(userRegDTO.getUsername());
        if(userVO!=null){
            return 2;
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
//        还有其他的字段  待续
//        所以userMapper也待续
        user.setStatusId(1);
        mapper.insertReg(user);
        return 1;
    }
}
